<?php

namespace app\api\controller;

use app\client\api\C_test;
use app\logic\common\L_secretIdKey;

class Test
{
    public $c_test;

    public function __construct()
    {
        $this->c_test = new C_test();
    }

    /**
     * 获取php版本
     */
    public function phpVersion()
    {
        $this->c_test->phpVersion();
    }

    /**
     * 测试
     */
    public function index()
    {
        $start=request()->param("start","1");
        $end=request()->param("end",20);
        $nums=request()->param("nums",150);
        $sums=request()->param("sums",30);
        $line_nums=request()->param("line_nums",8);

        $param=array(
            "start"=>$start,
            "end"=>$end,
            "nums"=>$nums,
            "sums"=>$sums,
            "line_nums"=>$line_nums,
        );

        $first=array();
        for($i=$start;$i<=$end;$i++){
            $first[]=$i;
        }

        $result=array();
        $result[]=$first;
        $result[]=$first;
        $shuzi= getCombinationToArr($result);

        $result=array();

        foreach($shuzi as $val){
            list($a,$b)=explode("@",$val);
            if($a+$b <= $sums && $a != $b){
                $result[]="{$a} + $b = ";
                if($a >=$b){
                    $result[]="{$a} - $b = ";
                }
            }
        }
        shuffle($result);
        $subResult=array_slice($result,0,$nums);

        $printReuslt=array();
        $i=0;
        foreach($subResult as $key=>$val){
            if($key%$line_nums==0){
                $i++;
            }
            $printReuslt[$i][]=$val;
        }
        $data = array();
        $data["request"] = request()->param();
        $data["printReuslt"] = $printReuslt;
        $data["param"] = $param;
        $data["sub_count"] = count($subResult);
        return view("api/test/test", $data);
    }

    /**
     * 分片下载
     */
    public function download()
    {
        $url = request()->param("url", "http://www.cms.com/upload/0e77e9c89bee49812e66f115c48eeafd.jpg");
        if ($url) {
            error_reporting(0);
            $buffer = 1024 * 3;//每个切片大小 单位字节
//            $filePath=str_replace("\\","/",runtimePath("cache/download/tmp"));
//            makeDir($filePath);
//            $filename=$filePath."test.jpg";
            ob_end_clean();
            $filename = "test.jpg";
//            header('Content-Type: ' . 'application/octet-stream;charset=utf-8;');
//            header('Content-Disposition: attachment; filename=' . $filename);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $begin = 0;
            do {
                $end = $begin + $buffer;
                curl_setopt($ch, CURLOPT_RANGE, $begin . '-' . $end);
                header('Content-Range:bytes ' . $begin . '-' . $end);
                $content = curl_exec($ch);
                $info = curl_getinfo($ch);
                if ($info['http_code'] == "206") {
                    echo $begin . "@" . $end . PHP_EOL;
//                    echo $content;
                    $begin = $end + 1;
                    unset($content);
                } else {
                    break;
                }
            } while (true);
        } else {
            echo "请输入url";
        }
    }

    /**
     * 获取文件信息
     * @param $url
     */
    public function getFileInfo($url)
    {
        // 初始化cURL会话
        $ch = curl_init($url);

        // 设置cURL选项
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, true); // 将结果保存到变量而不直接输出
        curl_setopt($ch, CURLOPT_NOBODY, true); // 只获取头部信息，不下载文件内容
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);// 单位 秒，超时限制。

        // 发送HTTP HEAD请求获取头部信息

        $header = curl_exec($ch);
        // 关闭cURL会话
        curl_close($ch);

        $headArr = explode("\r\n", $header);
        $headers = array();
        if ($headArr) {
            foreach ($headArr as $key => $val) {
                if ($val) {
                    list($itemKey, $itemVal) = array_pad(explode(":", $val), 2, "");
                    if ($itemVal) {
                        $headers[strtolower($itemKey)] = $itemVal;
                    }
                }
            }
        }
        return $headers;
    }


    /**
     * 获取获取安全key
     */
    public function getSecretKey()
    {
        $s = new L_secretIdKey();
        if (!$s->getPublicKey()||!$s->getSecretIdKey()){
            $err=$s->err;

        }else{
            echo $s->secretId."\r\n" ;
            echo $s->secretKey."\r\n" ;
        }



    }

}